/*#include "windows.h"*/

#include "updater.types.h"
#include "updater.errors.h"
#include "updater.h"
#include "updater.decode.h"
#include "updater.encode.h"
#include "updater.info.h"
#include "updater.utils.h"
#include "updater.hooks.h"
#include "updater.init.h"
INT gl_dbg_shift = 0;

#include "updater.w.h"
#include "updater.w.memory.h"
#include "updater.w.string.h"
#include "updater.w.system.h"
#include "updater.parser.h"


INT main(INT i_argn, CHAR ** ppc_args) {

  INT i_rv;
  ucore_t s_ucore;
  ENTRY();

/*  w_memset(&s_ucore, 0, sizeof(ucore_t)); */
  init_ucore(&s_ucore);
  s_ucore.env.argn = i_argn;
  s_ucore.env.args = ppc_args;
  

  if (i_argn < 3) 
  {
	if (i_argn == 2 && ppc_args[1][0] == 'v') 
	{
	  PRINT("updater-%d.%d\n", MAJOR, MINOR);
	  RETURN(0);
	}

	print_usage();
	RETURN( - ECANCELED );
  }

#ifdef USE_HOOKS
  if (OK != InitHooks(&s_ucore) )
  {
  	PRINT("Error on hooks initialization");
	RETURN( - ECANCELED );
  }
#endif /* USE_HOOKS */
  
  switch (ppc_args[1][0]) 
  {
    case 't':
	/* TODO: Free allocated */
    PRINT("Test\n");
    /* i_rv = ParseSpec (&s_ucore, ppc_args[2]); */
    RETURN(0);
    break;
	/* Create a new update */
	case 'c':
	  PRINT("Creating update\n");
	  #if 0
	  i_rv = ReadSpec (&s_ucore, ppc_args[3]);
	  if (i_rv) {
		PRINT ("Error on spec reading. Please check spec file '%s'\n", ppc_args[3]);
		
		RETURN( - ECANCELED );
	  }
		#endif


	  	w_memset(s_ucore.env.output_file_name, 0, ENV_NAME_SIZE);
		w_strncpy(s_ucore.env.output_file_name , ppc_args[2], ENV_NAME_SIZE - 1);

	  i_rv = ValidateFiles(&s_ucore);
      DD("file list is %p\n", (void *) s_ucore.file_list);
	  if (i_rv) 
      {
          DestroyFileList(&s_ucore);
          RETURN( i_rv );
      }
       
	  i_rv = CreatePackage(&s_ucore);
	  if ( i_rv != 0 ) {
          PRINT ("Error on files compressing stage\n");
          DestroyFileList(&s_ucore);
          RETURN( - ECANCELED );
	  }

/*	  i_rv = concat_files(ppc_args[2], s_fl);
	  if (i_rv != 0) {
		PRINT ("Error on concatination\n");
        DestroyFileList(s_fl);
		RETURN( - ECANCELED );
	  }
*/
      DestroyFileList(&s_ucore);
	  RETURN( 0 );
	  break; 
				  
	case 'd':
	{ /* TODO: Free allocated */
		PRINT("Updating\n");
        i_rv = UnpackPackage(&s_ucore, ppc_args[2]);
		/* i = decompress_file (args[2]); */
		RETURN( i_rv );
	}
#ifdef BE_VERBOSE

	case 'h':
	{	/* TODO: Free allocated */
		i_rv = print_header( ppc_args[2] );
		RETURN( i_rv );
	}

	case 'l':
	{ /* TODO: Free allocated */
		i_rv = list_files( ppc_args[2] );
		RETURN( i_rv );
	}

	case 'n':
	  i_rv = WriteDescriptionAndName ( ppc_args[2], NULL, ppc_args[3]);
	  if ( i_rv != 0 ) {
		PRINT("Error on writing update name\n");
	  }
	  break;

	case 'v':
	  /* TODO: Free allocated */
	  PRINT("updater-%d.%d\n", MAJOR, MINOR);
	  RETURN( 0 );
	  break;

#endif /* BE_VERBOSE */

	case 'm':
	  
	  i_rv = WriteDescriptionAndName ( ppc_args[2], ppc_args[3], NULL);
	  if ( i_rv != 0 ) {
		PRINT("Error on writing update	description\n");
	  }
	  break;

	default:
	/* TODO: Free allocated */
	  PRINT("Unknown parameter: %s\n", ppc_args[1]);
	  print_usage();
	  RETURN( -ENOSYS );
	  break;
  }
  RETURN( 0 );
}

